ColdFusion Article

Caching Queries to Disk or to Memory with ColdFusion


Table of Contents

  1. Introduction
  2. A Primer on ColdFusion's Built-in Query Caching
  3. A New Method for Caching Queries
  4. Caution When Caching to Disk
  5. Considerations and Conclusion

Caution When Caching to Disk

Developers with more database architecture experience may prefer database alternatives to this method of caching. For example, you might consider writing code that creates, populates, queries, and drops temporary tables in the databasestoring recordsets that require joining or filtering in larger queries. Letting your RDBMS do the heavy database work is often the best decision. However, this approach has a few caveats. If you want to write inline queries in your CFML code to create and drop temporary tables, you must configure your ColdFusion data source connection to use a database user that you set up with permissions to create and drop tables. This creates a bit of a security concern; a hacker could exploit your CFML code through this user and create or drop tables in your database inappropriately. To prevent this potential risk, you could move your create/drop SQL logic into stored procedures and grant execute permissions to your database user. While this is a solid workaround for the security risks I outline above, this method still requires that you have access, permissions, and know-how to create stored procedures in the database. For a primer on creating stored procedures, read Sam Neff's Learning Stored Procedure Basics in ColdFusion MX.

In looking at how my custom tag functions when caching to disk, some would argue that WDDX is an unacceptable means of storing these types of recordsets. WDDX, by design, is meant for exchanging small chunks of information between applications in a common format. However, WDDX also represents a means of creating a text-based snapshot of any ColdFusion object (simple value, query, structure, array, and so forth). Taking that one step further, you can write these text-based snapshots to disk (or memory) and retrieve them later. In most cases, ColdFusion performs very well when reading these files from the server's local disk. The serialization/deserialization of extremely large WDDX packets, however, often results in higher CPU utilization and a performance hit to ColdFusion. In many cases, the cost and execution time of deserializing a very large query result set from a cached WDDX packet loses the race against the execution time of the original query. As a developer, always evaluate query performance on a case-by-case basis before and after applying this caching solution. If you find that executing the original query outperforms deserializing the resulting WDDX packet, you should avoid using this disk-based caching approach and either try memory-based caching or seek other means of boosting your query performance.

The ideal situation for disk-based caching is when a query has many input parameters. Queries with more input parameters have the potential of generating a larger variant of recordsets. For example, consider a single query with input parameters of user_id, category_id, and media_type_id. If you have 5,000 records in your user table, 20 records in your category table, and 4 records in your media_type table, then this query might return 400,000 different recordsets. Now, if you wanted to cache this query into memory, you face the possibility of caching up to 400,000 different recordsets into memory. If each recordset was an average of 5K in size, this means you would use up to 2 GB of your server RAM. Additionally, this is only one queryadditional queries cached to memory would result in an even large number of recordsets stored in memory, chewing up more precious server RAM.

The upside of using my custom tag for disk-based caching is quite simple. By caching to disk, you eliminate concerns of how much memory you will use. Instead, you store cached queries on the server disk, where there is likely to be much more storage space available in comparison to server RAM and, therefore, much less concern over how much space cached queries use.